package cloud.xlh.my_system.middle_aware.mq;

import cloud.xlh.infrastructure.mq.EventDealReqMsg;
import cloud.xlh.infrastructure.utils.JSONObjectUtil;
import cloud.xlh.utils.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static cloud.xlh.my_system.middle_aware.mq.queue.QueueConfig.COMMON_QUEUE_NAME;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : xlh_osms
 * @ Author: XuLeHuang
 * @ Date: 2023/2/14 14:31
 * @ Description:
 */
@Component
public class RabbitMQEventConsumer implements EventConsumer {
    @Override
    public void consumeEvent(EventDealReqMsg eventDealReqMsg, Class aClass, Object... objects) {
        String businessCode = eventDealReqMsg.getBusinessCode();
        /**
         * 查库
         **/
        String simpleClassNameAndMethodName = "";
        doConsumeEvent(simpleClassNameAndMethodName , eventDealReqMsg.getBusinessParams());
    }

    private Object doConsumeEvent(String simpleClassNameAndMethodName, Object businessParams) {
        String[] simpleClassNameAndMethodNameArray = StringUtils.split(simpleClassNameAndMethodName, "/");
        String simpleClassName = simpleClassNameAndMethodNameArray[0];
        String simpleMethodName = simpleClassNameAndMethodNameArray[1];
        Object bean = SpringUtil.getBean(simpleClassName);
        Class<?> aClass = bean.getClass();
        Method method = null;
        try {
            method = aClass.getDeclaredMethod(simpleMethodName , EventDealReqMsg.class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        Object result = null;
        try {
            result = method.invoke(aClass, businessParams);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return result;
    }


    @RabbitListener(queues = COMMON_QUEUE_NAME)
    public void listenTopicQueue(String msg) {
        JSONObject msgBody = JSONObject.parseObject(msg);
        EventDealReqMsg eventDealReqMsg = (EventDealReqMsg) JSONObjectUtil.JSONObjectToJavaObject(msgBody, EventDealReqMsg.class);
        consumeEvent(eventDealReqMsg , eventDealReqMsg.getAClass());
        System.out.println("消费者接收到topic.queue1的消息:【" + msg + "】");
    }

        /*@RabbitListener(queues = COMMON_QUEUE_NAME)
    public void commonReceive(Message message, Channel channel) throws IOException, InterruptedException {
        System.out.println("收到消息：" + new String(message.getBody()));
        Thread.sleep(3000);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }*/


   /* @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue"),
            exchange = @Exchange(name = "topic.exchange", type = ExchangeTypes.TOPIC), key = "china.#"
    ))//意思是凡是有china可以的消息都能收到
    public void listenTopicQueue(String msg) {
        JSONObject msgBody = JSONObject.parseObject(msg);
        EventDealReqMsg eventDealReqMsg = (EventDealReqMsg) JSONObjectUtil.JSONObjectToJavaObject(msgBody, EventDealReqMsg.class);
        consumeEvent(eventDealReqMsg , eventDealReqMsg.getAClass());
        System.out.println("消费者接收到topic.queue1的消息:【" + msg + "】");
    }*/
}
